package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.Model.AnswerModel;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionType;
import cc.eddic.pratice.programmingparadigms.exam.domain.Category;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.SubCategory;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

@Slf4j
public class QuestionBankServiceCxyImpl extends QuestionBankServiceImpl implements QuestionBankService {
    @Override
    public List<Question> importQuestions(String questionTextFile) {
        List<Question> qList = new ArrayList<>();
        String questions = getQuestionFileContent();
        String[] questionsArray = questions.split("\n");
        for (String qStr : questionsArray) {
            if (qStr.length() == 0) {
                continue;
            }
            if (Character.isDigit(qStr.charAt(0))) {
                String[] subQuestion = qStr.split("、");
                qList.add(new Question(Integer.parseInt(subQuestion[0]), subQuestion[1], QuestionType.SINGLE_CHOICE));
            }
            if ((qStr.charAt(0) >= 'a' && qStr.charAt(0) <= 'z') || (qStr.charAt(0) >= 'A' && qStr.charAt(0) <= 'Z')) {
                String[] subQuestion = qStr.split("、");
                if (!qList.isEmpty()) {
                    qList.get(qList.size() - 1).add(new Option(subQuestion[1], subQuestion[0].charAt(0), false));
                }
            }
        }
        return qList;
    }

    @Override
    public List<AnswerModel> readCorrectAnswers(String correctAnswerTextFile) {
        String fileContent = getAnswerFileContent();
        List<AnswerModel> answerList = new ArrayList<>();
        String[] list = fileContent.split("\n");
        for (int i = 0; i < list.length; i = i + 2) {
            String[] questionNo = list[i].split("\t");
            String[] questionAnswer = list[i + 1].split("\t");
            for (int j = 0; j < questionNo.length; j++) {
                AnswerModel answer = new AnswerModel(Integer.parseInt(questionNo[j]), questionAnswer[j].charAt(0));
                answerList.add(answer);
            }
        }
        return answerList;
    }

    @Override
    public List<Question> importCategories(String categoryExcelFile) {
        QuestionBankServiceZyzImpl questionBankServiceZyz = new QuestionBankServiceZyzImpl();
        return questionBankServiceZyz.importCategories(categoryExcelFile);
    }

    @Override
    public List<Question> importCorrectAnswers(String correctAnswerTextFile) {
        List<Question> questionList = importQuestions("");
        Map<Integer, Character> answer = readCorrectAnswers2("");
        for (Question q : questionList){
            if (!answer.containsKey(q.getNo())) continue;
            char choice = answer.get(q.getNo());
            Option option = q.getOptions().stream()
                    .filter(ch -> ch.getChoice() == choice)
                    .findFirst()
                    .orElseThrow(() -> new IllegalArgumentException("没找到捏"));
            option.changeToCorrectAnswer();
        }
        return questionList;
    }

    @Override
    public Map<Integer, Character> readCorrectAnswers2(String correctAnswerTextFile) {
        Map<Integer, Character> result = new HashMap<>();
        String[] lines = getAnswerFileContent().split("\n");
        for (int i=0 ;i < lines.length; i=i+2){
            String[] nos = lines[i].split("\t");
            String[] choices = lines[i + 1].split("\t");
            for (int j = 0; j < nos.length; j++) {
                String no = nos[j];
                String choice = choices[j];
                result.put(Integer.parseInt(no), choice.charAt(0));
            }
        }
        return result;
    }
}
